﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.OData;
using System.Web.Http.OData.Routing;
using ICD10WebApiOData.DTO;
using ICD10WebApiOData.Models;

namespace ICD10WebApiOData.Controllers
{
    /*
    To add a route for this controller, merge these statements into the Register method of the WebApiConfig class. Note that OData URLs are case sensitive.

    using System.Web.Http.OData.Builder;
    using ICD10WebApiOData.Models;
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<ChapterContain>("ICDChapterContain");
    config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
    */
    public class ICDChapterContainController : ODataController
    {
        private ICD10BEntities db = new ICD10BEntities();

        // GET odata/ICDChapterContain
        [Queryable]
        public IQueryable<ChapterContain> GetICDChapterContain()
        {
            return db.ChapterContains;
        }

        // GET odata/ICDChapterContain(5)
        [Queryable]
        public SingleResult<ChapterContain> GetChapterContain([FromODataUri] int key)
        {
            return SingleResult.Create(db.ChapterContains.Where(chaptercontain => chaptercontain.ID == key));
        }

        [HttpPost]
        public IQueryable<ICDChapterContain> GetChapterContainByChapID(ODataActionParameters parameters)
        {
            string chapID = (string)parameters["ChapID"];
            var result = from icd in db.ChapterContains
                         where icd.ChapterID == chapID
                         select new ICDChapterContain()
                         {
                             ID = icd.ID,
                             ChapterID = icd.ChapterID,
                             ChapterContainID = icd.ChapterContainID,
                             Description = icd.Description,
                             Include = icd.Include
                         };
            return result;
        }       

        // PUT odata/ICDChapterContain(5)
        public IHttpActionResult Put([FromODataUri] int key, ChapterContain chaptercontain)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != chaptercontain.ID)
            {
                return BadRequest();
            }

            db.Entry(chaptercontain).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChapterContainExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(chaptercontain);
        }

        // POST odata/ICDChapterContain
        public IHttpActionResult Post(ChapterContain chaptercontain)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.ChapterContains.Add(chaptercontain);
            db.SaveChanges();

            return Created(chaptercontain);
        }

        // PATCH odata/ICDChapterContain(5)
        [AcceptVerbs("PATCH", "MERGE")]
        public IHttpActionResult Patch([FromODataUri] int key, Delta<ChapterContain> patch)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ChapterContain chaptercontain = db.ChapterContains.Find(key);
            if (chaptercontain == null)
            {
                return NotFound();
            }

            patch.Patch(chaptercontain);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChapterContainExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(chaptercontain);
        }

        // DELETE odata/ICDChapterContain(5)
        public IHttpActionResult Delete([FromODataUri] int key)
        {
            ChapterContain chaptercontain = db.ChapterContains.Find(key);
            if (chaptercontain == null)
            {
                return NotFound();
            }

            db.ChapterContains.Remove(chaptercontain);
            db.SaveChanges();

            return StatusCode(HttpStatusCode.NoContent);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ChapterContainExists(int key)
        {
            return db.ChapterContains.Count(e => e.ID == key) > 0;
        }
    }
}
